package com.ebt.m.policy.view;

import android.content.Context;
import android.util.AttributeSet;

import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.data.rxModel.apibean.FilterDataBeanItem;
import com.ebt.m.policy.bean.EventPrams;
import com.ebt.m.policy.presenter.FilterPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;


/**
 * 所有产品
 *
 * @author Damon.Han
 */
public class FilterListView extends BaseListView<FilterPresenter> {


    public FilterListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FilterListView(Context context) {
        this(context, null);
    }

    public FilterListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mRefreshLayout.setRefreshNeeded(false);
    }


    @Override
    protected ISectionFactory createViewFactory() {
        return viewType -> {
            if (viewType == FilterPresenter.DATA_VIEW_TITLE) {
                return new FilterTitleView(getContext());
            } else if (viewType == FilterPresenter.DATA_VIEW_TYPE) {
                FilterContentView appsViewItem = new FilterContentView(getContext());
                return appsViewItem;
            }
            return null;
        };
    }


    @Override
    protected FilterPresenter createPresenter() {
        return new FilterPresenter(getContext(), this);
    }

    @Override
    public void loadNext() {

    }


    @Override
    public void update(Object... args) {
        super.update(args);
        mPresenter.loadNew();
    }

    public void pressOk() {
        HashMap<String, String> params = new HashMap<>();
        for (int i = 0; i < mLineAdpater.getItemCount(); i++) {
            if (mRecyclerView.getChildAt(i) instanceof FilterContentView) {
                FilterContentView view = (FilterContentView) mRecyclerView.getChildAt(i);
                FilterDataBeanItem filterDataBeanItem = view.getChecked();
                if (filterDataBeanItem != null) {
                    params.put(filterDataBeanItem.getKey(), filterDataBeanItem.getValue() + "");
                }
            }
        }
        EventBus.getDefault().post(new EventPrams(0, params));
    }

    /**
     * 重置
     */
    public void reset() {
        for (int i = 0; i < mLineAdpater.getItemCount(); i++) {
            if (mRecyclerView.getChildAt(i) instanceof FilterContentView) {
                FilterContentView view = (FilterContentView) mRecyclerView.getChildAt(i);
                view.reset();
            }
        }
    }
}
